package main

import (

	//"web/thrift"

	"web/conf"
	"web/pool"
	"web/route"
	"web/tsuna"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

func main() {

	//初始化配置
	conf.ConfigInit()

	//初始化连接池
	pool.PoolInit()

	//连接hbase
	tsuna.ConnectHBase(conf.Zoohome)

	//app := iris.New()
	app := newApp()

	//应用App设置
	configation(app)
	//mvc
	mvcHandle(app)
	//plain
	route.PlaHandle(app)
	//run
	app.Run(iris.Addr(conf.Port)) //8080 监听端口

}

//构建App
func newApp() *iris.Application {

	app := iris.New()

	//设置日志级别  开发阶段为debug
	app.Logger().SetLevel(conf.Loglev)

	//注册静态资源
	app.HandleDir("/mangzhe/static", "./static")
	app.HandleDir("/manage/static", "./static")
	app.HandleDir("/mangzhe/img", "./static/img")
	app.HandleDir("/mangzhe/css", "./static/css")

	//注册视图文件
	app.RegisterView(iris.HTML("./static", ".html"))
	app.Get("/mangzhe", func(context iris.Context) {
		context.View("index.html")
	})

	return app
}

/**
 * 项目设置
 */
func configation(app *iris.Application) {

	//配置 字符编码
	app.Configure(iris.WithConfiguration(iris.Configuration{
		Charset: conf.Charset,
	}))

	//错误配置
	//未发现错误
	app.OnErrorCode(iris.StatusNotFound, func(context iris.Context) {
		context.JSON(iris.Map{
			"errmsg": iris.StatusNotFound,
			"msg":    " not found ",
			"data":   iris.Map{},
		})
	})

	app.OnErrorCode(iris.StatusInternalServerError, func(context iris.Context) {
		context.JSON(iris.Map{
			"errmsg": iris.StatusInternalServerError,
			"msg":    " interal error ",
			"data":   iris.Map{},
		})
	})
}

//MVC 架构模式处理
func mvcHandle(app *iris.Application) {
	//启用session
	//sessManager := sessions.New(sessions.Config{
	//	Cookie:  "sessioncookie",
	//	Expires: 24 * time.Hour,
	//})

	//UserController handle
	mvc.New(app).Handle(new(route.UserController))

	//UserController路由组的mvc处理
	mvc.Configure(app.Party("/user"), func(context *mvc.Application) {
		//context是*mvc.Application类型
		//和这个 mvc.New(app) 一样
		context.Handle(new(route.UserController))
	})
}
